<?php
/**
 * Created by PhpStorm.
 * User: Raytine
 * Date: 2018/8/30
 * Time: 11:46
 */
namespace PddWechat\Models\User\Service;

use Dai\Framework\Base\BaseSessionInfo;
use PddWechat\Models\User\Data\BindBankCardData;
use PddWechat\Models\User\DataObject\UserBankCardDo;
use PddWechat\Models\User\Param\BindBankCardParam;

class BindBankCard
{

    public function __construct()
    {
        $this->bindBankCardData = new BindBankCardData();
    }

    public function execute($basePageInfo)
    {
        /** @var BaseSessionInfo $sessionData */
        $sessionData = $basePageInfo->sessionInfo;
        $uid = $sessionData->uid;
        /** @var BindBankCardParam $params*/
        $params = $basePageInfo->params;

        $userBankCardDo = new UserBankCardDo();
        $userBankCardDo->uid = $uid;
        $userBankCardDo->bankCard = $params->bankCard;
        $userBankCardDo->userName = $params->userName;
        $userBankCardDo->phone = $params->phone;

        $cardInfo = $this->bindBankCardData->selectByUid($uid);
        if(empty($cardInfo)) {
            return $this->bindBankCardData->insert($userBankCardDo);
        }else {
            $userBankCardDo->id = $cardInfo->id;
            return $this->bindBankCardData->edit($userBankCardDo);
        }
    }
}